package com.tyk.bookstore.back.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.system.model.query.MenuAddQuery;
import com.tyk.bookstore.back.system.model.query.MenuListQuery;
import com.tyk.bookstore.back.system.model.query.MenuUpdateQuery;
import com.tyk.bookstore.back.system.model.vo.MenuListVo;
import com.tyk.bookstore.back.system.model.vo.MenuSuggestionVo;
import com.tyk.bookstore.back.system.model.vo.MenuVo;
import com.tyk.bookstore.back.system.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "系统管理/菜单管理", description = "菜单管理")
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("api/system/menu")
public class MenuController {

    private final MenuService menuService;

    @Operation(summary = "查询菜单列表", description = "查询菜单列表")
    @SaCheckPermission("system:menu:list")
    @GetMapping("list")
    public R<List<MenuListVo>> getList(
            @Validated MenuListQuery query
    ) {
        return R.ok(menuService.getList(query));
    }

    @Operation(summary = "查询菜单详情", description = "查询菜单详情")
    @SaCheckPermission("system:menu:get")
    @GetMapping("{id}")
    public R<MenuVo> getById(
            @Validated @Min(0) @PathVariable("id") Long id
    ) {
        return R.ok(menuService.getById(id));
    }

    @Operation(summary = "新增菜单", description = "新增菜单")
    @SaCheckPermission("system:menu:add")
    @PostMapping("")
    public R<Object> add(
            @Validated @RequestBody MenuAddQuery query
    ) {
        menuService.add(query);
        return R.ok();
    }

    @Operation(summary = "根据ID修改菜单信息", description = "根据ID修改菜单信息")
    @SaCheckPermission("system:menu:update")
    @PutMapping("")
    public R<Object> updateById(
            @Validated @RequestBody MenuUpdateQuery query
    ) {
        menuService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "根据ID删除菜单信息", description = "根据ID删除菜单信息")
    @SaCheckPermission("system:menu:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Validated @Min(0) @PathVariable("id") Long id
    ) {
        menuService.deleteById(id);
        return R.ok();
    }

    @Operation(summary = "查询菜单建议列表", description = "查询菜单建议列表")
    @SaCheckPermission("system:menu:suggestion:list")
    @GetMapping("suggestion/list")
    public R<List<MenuSuggestionVo>> getSuggestionList(
            Boolean showMenuItem
    ) {
        return R.ok(menuService.getSuggestionList(showMenuItem));
    }
}
